package com.jnshu.controller;

import com.jnshu.dto.user.MyInfoDto;
import com.jnshu.dto.user.MyPersonDto;
import com.jnshu.pojo.User;
import com.jnshu.service.PersonService;
import com.jnshu.tools.binding.JsonUtil;
import com.jnshu.tools.resultUtils.PageBean;
import com.jnshu.tools.resultUtils.RUtil;
import com.jnshu.tools.resultUtils.ResultBean;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author xmp
 * @date 2019/8/18 4:26
 */
@Log
@RestController
public class PersonController {
    @Autowired
    PersonService personService;

    @PostMapping(value ="/u/my")
    public ResultBean myPerson(Long id){
        log.info("传入个人中心的用户id为："+id);
        MyPersonDto myPersonDto= personService.selectMyPersonById(id);
        return RUtil.ok(myPersonDto);
    }

    @PostMapping(value ="/u/my/information")
    public ResultBean myPersonInfo(Long id){
        log.info("传入个人资料的用户id为："+id);
        MyInfoDto myInfoDto= personService.selectMyInfoById(id);
        log.info("myInfoDto:"+myInfoDto);
        return RUtil.ok(myInfoDto);
    }

    @PutMapping(value = "/u/my/information",produces = {"application/json;charset=UTF-8"})
    public ResultBean myPersonUpdate(@RequestBody User user){
        log.info("传入的user为："+ JsonUtil.toJsonString(user));
        return personService.updateMyInfo(user);
    }

    @GetMapping(value = "/u/my/binding")
    public ResultBean bindingInfo(@RequestParam("id")Long id){
        log.info("传入的用户id为："+id);
        return personService.selectUserBindingInfo(id);
    }

    @GetMapping("/u/my/collection")
    public PageBean myCollection(@RequestParam(name = "classify",required = false)int classify,
                                 @RequestParam(name = "pageNum",defaultValue = "1")int pageNum,
                                 @RequestParam(name = "pageSize",defaultValue = "10")int pageSize,
                                 @RequestParam(name = "userId",required = false) long userId){
        log.info("传入的类型是："+classify+",传入的资料收藏列表页码是："+pageNum+",每页几个数据："+pageSize+",用户ID是："+userId);
        return personService.getCollectCourse(classify,pageNum,pageSize,userId);
    }

    @DeleteMapping(value = "/u/my/collection")
    public ResultBean deleteCollection(@RequestParam(value = "courseId") long courseId, @RequestParam(value = "userId") long userId) {
        log.info("传入的前台资料收藏id：" + courseId+",用户id："+userId);
        return personService.deleteCollection(courseId,userId);
    }
    @GetMapping(value ="/u/my/history")
    public ResultBean myHistory(@RequestParam(value = "type",required = false)Integer type,
                                   @RequestParam(value = "pageNum",required = false,defaultValue ="1") Integer pageNum,
                                   @RequestParam(value = "pageSize",required =false,defaultValue ="10") Integer pageSize,
                                   @RequestParam(value = "userId",required = false) long userId){
        log.info("传入的类型是："+type+",传入的资料收藏列表页码是："+pageNum+",每页几个数据："+pageSize+",用户ID是："+userId);
        return personService.getHistory(type,pageSize,pageNum,userId);
    }
    @DeleteMapping(value = "/u/my/history")
    public ResultBean deleteHistory(@RequestParam(value = "userId") long userId) {
        log.info("传入的用户id："+userId);
        return personService.deleteHistory(userId);
    }

    @DeleteMapping(value = "/u/my/collection/more")
    public ResultBean deleteMoreCollection(@RequestParam(value = "userId") long userId,
                                        @RequestParam(value = "courseId") String courseId,
                                        @RequestParam(value = "status") String status) {
        log.info("前台负载均衡传入的用户id："+userId+",传入的资料id："+courseId+",传入的状态"+status);
        return personService.deleteMoreCollection(userId,courseId,status);
    }
}
